package com.lyl.servicecms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyl.commonutils.R;
import com.lyl.servicecms.entity.Banner;
import com.lyl.servicecms.service.BannerService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author 雷艳玲
 * @since 2022-06-17
 */
@RestController
@RequestMapping("/servicecms/banner")
public class BannerController {

    @Autowired
    private BannerService bannerService;

    @ApiOperation(value = "获取Banner分页列表")
    @PostMapping("{page}/{limit}")
    public R index(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @RequestBody(required = false) Banner banner) {

        Page<Banner> pageParam = new Page<>(page, limit);
        bannerService.page(pageParam,null);
        return R.ok().data("items", pageParam.getRecords()).data("total", pageParam.getTotal());
    }



    @ApiOperation(value = "根据id修改状态")
    @PostMapping("updateStatusById/{id}/{bannerStatus}")
    public R updateStatusById(@PathVariable Long id,@PathVariable Boolean bannerStatus) {
        if( bannerService.updateStatusById(id,bannerStatus)) {
            return R.ok().message("操作成功");
        }
        return R.error().message("操作失败");
    }


    @ApiOperation(value = "获取Banner")
    @GetMapping("get/{id}")
    public R get(@PathVariable String id) {
        Banner banner = bannerService.getById(id);
        return R.ok().data("item", banner);
    }

    @ApiOperation(value = "新增Banner")
    @PostMapping("save")
    public R save(@RequestBody Banner banner) {
       if( bannerService.addBanner(banner)) {
           return R.ok().message("添加成功");
       }
        return R.error().message("添加失败");
    }

    @ApiOperation(value = "修改Banner")
    @PutMapping("update")
    public R updateById(@RequestBody Banner banner) {
        if(   bannerService.updateBanner(banner)){
            return R.ok().message("修改成功");
        }
        return R.error().message("修改失败");
    }

    @ApiOperation(value = "删除Banner")
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable Long id) {
        if(   bannerService.deleteBanner(id)){
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }
}

